import pandas as pd

df = pd.read_csv('static/data/movie_box_rate.csv')
df_line = pd.read_csv('static/data/liner_Regression.csv')


def box_top10_data():
    df_box_top10 = df[['movie_name', 'movie_box_split_unit']].groupby('movie_name').sum().sort_values(
        'movie_box_split_unit', ascending=False).reset_index().head(10)
    name = df_box_top10['movie_name'].values.tolist()
    data = df_box_top10['movie_box_split_unit'].values.tolist()
    return {
        'name': name,
        'data': [round(i, 2) for i in data]
    }


def box_top1_movie_daily_trends():
    df_total_box = df[['movie_box_split_unit','cur_date']].groupby(['cur_date']).sum().reset_index()
    return {
        'name': df_total_box['cur_date'].values.tolist(),
        'data': df_total_box['movie_box_split_unit'].values.tolist()
    }


def movie_box_ratio():
    df_box_top10 = df[['movie_name', 'movie_box_split_unit']].groupby('movie_name').sum().sort_values(
        'movie_box_split_unit', ascending=False).reset_index().head(10)
    total_box = round(df[['movie_box_split_unit']].sum().values.tolist()[0], 2)
    other_box = round(total_box - df_box_top10['movie_box_split_unit'].sum(), 2)
    box_ratio_list = [{'value': round(i[1], 2), 'name': i[0]} for i in df_box_top10.values.tolist()]
    box_ratio_list.append({'value': other_box, 'name': '其他'})
    return {
        'data': box_ratio_list
    }


def movie_box_and_show_count_scatter():
    line_data = df_line.values.tolist()
    df_scatter = df[['movie_box_split_unit', 'movie_show_count']]
    df_scatter = df_scatter[(df_scatter['movie_box_split_unit'] > 10) & (df_scatter['movie_show_count'] > 10)]
    scatter_data = df_scatter.values.tolist()
    return {
        'line_data': line_data,
        'scatter_data': scatter_data
    }


def top1_movie_heat_trends():
    df_box_top10 = df[['movie_name', 'movie_box_split_unit']].groupby('movie_name').sum().sort_values(
        'movie_box_split_unit', ascending=False).reset_index()
    top_one_movie_name = df_box_top10.head(1)['movie_name'].values.tolist()[0]
    top_one_daily_trends = df[df['movie_name'] == top_one_movie_name][
        ['cur_date', 'movie_box_split_unit']].values.tolist()
    return {
        'data': top_one_daily_trends
    }
